package com.wonderful.eyepetizer.ui.pages.hot

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Divider
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.TabRowDefaults.tabIndicatorOffset
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.lifecycle.viewmodel.compose.viewModel
import com.wonderful.eyepetizer.R
import com.wonderful.eyepetizer.data.entity.bean.Tab
import com.wonderful.eyepetizer.ui.pages.daily.RankItemWidget
import com.wonderful.eyepetizer.ui.theme.NormalItemColor
import com.wonderful.eyepetizer.view.TitleBarWidget
import kotlinx.coroutines.launch

/**
 * Desc: 热门
 * @Author: wonderful
 * @Time: 2024/1/19 16:47
 */

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HotPage(viewModel: HotViewModel = viewModel()) {
    val pagerState = rememberPagerState(pageCount = { viewModel.tabList.size })
    val coroutineScope = rememberCoroutineScope()

    LaunchedEffect(key1 = pagerState.currentPage, block = {
        viewModel.tabIndex.intValue = pagerState.currentPage
    })

    Column(modifier = Modifier.fillMaxSize()) {
        TitleBarWidget(title = stringResource(id = R.string.hot))
        HotTabRowWidget(
            tabs = viewModel.tabList,
            tabIndex = viewModel.tabIndex.intValue,
            onTabClick = {
                coroutineScope.launch {
                    viewModel.tabIndex.intValue = it
                    pagerState.animateScrollToPage(it)
                }
            })
        HotTabPageWidget(
            tabs = viewModel.tabList,
            pagerState = pagerState,
            modifier = Modifier
                .weight(1f)
                .zIndex(-1f)
        )

    }
}

@Composable
fun HotTabRowWidget(tabs: List<Tab>, tabIndex: Int, onTabClick: (Int) -> Unit) {
    if (tabs.isEmpty()) return

    TabRow(selectedTabIndex = tabIndex, containerColor = Color.White, indicator = {
        Box(
            modifier = Modifier.tabIndicatorOffset(it[tabIndex]),
            contentAlignment = Alignment.Center
        ) {
            Divider(
                modifier = Modifier.width(80.dp),
                thickness = 3.dp,
                color = LocalContentColor.current
            )
        }
    }) {
        tabs.forEachIndexed { index, tab ->
            Tab(
                selected = tabIndex == index,
                onClick = { onTabClick(index) },
                text = { Text(text = tab.name, fontSize = 14.sp) },
                selectedContentColor = Color.Black,
                unselectedContentColor = NormalItemColor
            )
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HotTabPageWidget(tabs: List<Tab>, pagerState: PagerState, modifier: Modifier = Modifier) {
    HorizontalPager(state = pagerState, modifier = modifier) {
        TabHotWidget(tab = tabs[it])
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun TabHotWidget(tab: Tab) {
    val viewModel: HotViewModel = viewModel(key = tab.name)
    LaunchedEffect(key1 = Unit, block = {
        viewModel.getTabData(url = tab.apiUrl)
    })

    val pullRefreshState = rememberPullRefreshState(
        refreshing = viewModel.refreshing.value,
        onRefresh = { viewModel.getTabData(tab.apiUrl, true) })
    Box(modifier = Modifier.pullRefresh(pullRefreshState)) {
        LazyColumn(content = {
            items(viewModel.itemList.size) {
                viewModel.itemList[it].data?.let { item -> RankItemWidget(itemData = item) }
            }
        })
        PullRefreshIndicator(
            refreshing = viewModel.refreshing.value,
            state = pullRefreshState,
            modifier = Modifier.align(Alignment.TopCenter)
        )
    }
}